<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/vue.js"></script>
</head>
<body>
    <div id="app">
        <msg-list :list="msgList"></msg-list>
        <add-msg :agou="receive"></add-msg>
    </div>
    <template id="addMsg">
        <div>
            <p>
                <textarea name="" id="" cols="30" rows="10" v-model="content"></textarea>
            </p>
            <button @click="postMsg">发布</button>
        </div>
    </template>
    <template id="msgList">
        <div>
            <ul>
                <li v-for="(item,index) in list" :key="index">{{item.content}}</li>
            </ul>
        </div>
    </template>
</body>
<script>
    let AddMsg={
        name:"AddMsg",
        template:"#addMsg",
        props:{
            agou:Function
        },
        data(){
            return{
                content:""
            }
        },
        methods:{
            postMsg(){
                alert(this.content)
                let randomId=Math.ceil(Math.random()*100)
                let msg={
                    id:randomId,
                    content:this.content
                }
                this.agou(msg)
            }
        }
    }
    let MsgList={
        name:"MsgList",
        template:"#msgList",
        props:{
            list:Array
        }
    }
    const app=new Vue({
        el:"#app",
        data:{
            msgList:[]
        },
        methods:{
            receive(msg){
                this.msgList.unshift(msg)
            }
        },
        components:{
            AddMsg,
            MsgList
        }

    })
</script>
</html>